package wwj.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class SqlUtil {

    static Connection conn=null;
    public  static Connection getConnection() {
        //1.导入jar包
        //2.注册驱动
        String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=casualshop;encrypt=false";
        String userName = "sa";
        String userPwd = "1234";
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(dbURL, userName, userPwd);
            System.out.println("连接数据库成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("连接失败");
        }
        return conn;
//    }
//
//    public static void main(String[] args) {
//        Connection conn=SqlUtil.getConnection();
//        try {
//            Statement statement = conn.createStatement();
//            ResultSet resultSet = statement.executeQuery("select * from commodity");
//            while (resultSet.next()) {
//                int id = resultSet.getInt("id");
//                String age = resultSet.getString("info");
//                System.out.println("id:" + id + "   " + "age:" + age);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
////        }
//        }
    }

}


